import {request} from "../../utils/request";
import config from "../../utils/config";

const app = getApp();
Page({
    data: {
        picUrl: config.picUrl,
        url: config.url,
        custom: wx.getMenuButtonBoundingClientRect(),
        user:wx.getStorageSync('userInfo'),
        formData: {album_id: 1},
        brand_id:null,
        name:'',
        bannerList:[],
        content:'',
        topics:[],
        lat:'',
        lng:'',
        province:'',
        city:'',
        county:'',
        recommend:''
    },
    onLoad: function (options) {
        this.setData({brand_id:this.data.user.id})
    },
    changeValue(e) {
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]: value})
    },
    uploadBanner() {
        this.upload(9 - this.data.bannerList.length, res => {
            var bannerList = this.data.bannerList
            bannerList.push(res.data.src)
            this.setData({bannerList})
        })
    },
    delBanner(e) {
        var index = e.currentTarget.dataset.index,
            bannerList = this.data.bannerList
        var res = bannerList.splice(index, 1)
        this.setData({bannerList})
    },
    upload(count, callback) {
        wx.showLoading()
        wx.chooseImage({
            count: count,
            sizeType: ['compressed', 'original'],
            success: res => {
                for (let v of res.tempFiles) {
                    wx.uploadFile({
                        url: this.data.url + '/system/api/uploadImage',
                        filePath: v.path,
                        name: 'file',
                        formData: this.data.formData,
                        success: res => {
                            wx.hideLoading()
                            callback(JSON.parse(res.data))
                        }
                    })
                }
            },
            fail: err => {
                wx.hideLoading()
            }
        })

    },
    delTopics(e){
        var index = e.currentTarget.dataset.index,
            topics = this.data.topics
        topics.splice(index,1)
        this.setData({topics})
    },
    addTopics(){
        wx.showModal({
            title:'添加一个引人注目的标签吧',
            editable:true,
            success:res=>{
                if(res.content == '') return false
                var topics = this.data.topics
                topics.push(res.content)
                this.setData({topics})
            }
        })
    },
    submit(){
        request({
            url:"/miniprogram/subcircle/addSubcircle",
            data:{
                brand_id:this.data.brand_id,
                // name:this.data.name,
                banner:this.data.bannerList.join(','),
                content:this.data.content,
                topics:this.data.topics.join(','),
                lat:this.data.lat,
                lng:this.data.lng,
                province:this.data.province,
                city:this.data.city,
                county:this.data.county,
                recommend:this.data.recommend
            },
            type:'post'
        }).then(res=>{
            if(res.code == 200){
                wx.showToast({
                    title:'发布成功',
                    icon:"none",
                    mask:true,
                    success:res=>{
                        setTimeout(()=>{wx.navigateBack()},1500)
                    }
                })
            }
        })
    },
    getAddress(){
        wx.showLoading()
        wx.getFuzzyLocation({
            type:'wgs84',
            success:res=>{
                console.log(res)
                // if(res.errno != 0){
                //     wx.hideLoading()
                //     wx.showToast({title:res.errMsg,icon:'none'})
                //     return false
                // }
                this.setData({lat:res.latitude,lng:res.longitude})
                var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=LBJBZ-AMQ66-PHRSD-MXHZK-UFYW3-DZBYH";
                wx.request({
                    url:getAddressUrl,
                    success:rel=>{
                        wx.hideLoading()
                        rel = rel.data
                        this.setData({
                            province:rel.result.address_component.province,
                            city:rel.result.address_component.city,
                            county:rel.result.address_component.district,
                            recommend:rel.result.formatted_addresses.recommend,
                        })
                    }
                })
            },
            fail(e) {
                wx.hideLoading()
            }
        })
    }
});